package mi.payment.ui.director;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.ui.admin.DownloadManagementActivity;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.other.LoginActivity;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.HintDialog;

import static mi.payment.R.id.img_icon;

/**
 * 管理操作员首页
 *
 * @author lj on 2017/1/7.
 */
public class AdministratorUserHomePageActivity extends ParentActivity implements View.OnClickListener {

    private HintDialog hintDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_administrator_user);
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        LinearLayout adminPasswordModificationLayout, ordinaryUserAdministrationLayout, autoLoginManageLayout, downloadManageLayout;
        RelativeLayout loginOutLayout;
        TextView titleText;
        TextView itemText;
        ImageView itemImage;

        adminPasswordModificationLayout = (LinearLayout) findViewById(R.id.home_page_admin_password_modification);
        ordinaryUserAdministrationLayout = (LinearLayout) findViewById(R.id.home_page_ordinary_user_administration);
        autoLoginManageLayout = (LinearLayout) findViewById(R.id.home_page_auto_login);
        downloadManageLayout = (LinearLayout) findViewById(R.id.home_page_download);

        boolean isSupport = mSupportController.isSupportDownloadInAdministratorHome();
        if (!isSupport) {
            downloadManageLayout.setVisibility(View.INVISIBLE);
        }

        itemText = (TextView) adminPasswordModificationLayout.findViewById(R.id.text_type);
        itemText.setText(getResources().getString(R.string.password_modification_mange));

        itemText = (TextView) ordinaryUserAdministrationLayout.findViewById(R.id.text_type);
        itemText.setText(getResources().getString(R.string.ordinary_user_administration));

        itemText = (TextView) autoLoginManageLayout.findViewById(R.id.text_type);
        itemText.setText(getResources().getString(R.string.auto_Login_Manage));

        itemText = (TextView) downloadManageLayout.findViewById(R.id.text_type);
        itemText.setText(getResources().getString(R.string.download_function));

        itemImage = (ImageView) adminPasswordModificationLayout.findViewById(img_icon);
        itemImage.setImageResource(R.drawable.home_password_management);

        itemImage = (ImageView) ordinaryUserAdministrationLayout.findViewById(img_icon);
        itemImage.setImageResource(R.drawable.home_operators_manage);

        itemImage = (ImageView) autoLoginManageLayout.findViewById(img_icon);
        itemImage.setImageResource(R.drawable.all_ic_auto_login_manage);

        itemImage = (ImageView) downloadManageLayout.findViewById(img_icon);
        itemImage.setImageResource(R.drawable.setting_system);

        loginOutLayout = (RelativeLayout) findViewById(R.id.login_out);
        titleText = (TextView) findViewById(R.id.home_page_title_text);
        titleText.setText(getResources().getString(R.string.competent_operator));

        adminPasswordModificationLayout.setOnClickListener(this);
        ordinaryUserAdministrationLayout.setOnClickListener(this);
        autoLoginManageLayout.setOnClickListener(this);
        downloadManageLayout.setOnClickListener(this);

        loginOutLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.home_page_admin_password_modification:
                // 密码修改
                Intent intent = new Intent(this, UpdatePasswordActivity.class);
                intent.putExtra("user_info", mUserInfoModel.queryUserInfoById("00"));
                intent.putExtra("who_update", 2);
                startActivity(intent);
                break;
            case R.id.home_page_ordinary_user_administration:
                // 操作员管理
                openActivity(OperatorAdministratorActivity.class);
                break;
            case R.id.login_out:
                // 签退
                showHintDialog();
                break;
            case R.id.home_page_auto_login:
                // 自定登录管理
                openActivity(OperatorAdminAutoLogin.class);
                break;
            case R.id.home_page_download:
                // 其他管理设置
                //下载功能
                startActivity(new Intent(this,DownloadManagementActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            showHintDialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showHintDialog() {
        hintDialog = DialogCreater.createHintDialog(this, R.drawable.state_warning, getString(R.string.cancel),
                getString(R.string.ok), getString(R.string.ensure_exit),
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        hintDialog.dismiss();
                    }

                }, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        hintDialog.dismiss();
                        Intent intent = new Intent(AdministratorUserHomePageActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }

                }, false);
        hintDialog.show();
    }

}
